#include "Handler.h"
#include "NetworkDef.h"

Handler::Handler()
{
	
}

Handler::~Handler()
{
}

void Handler::start()
{
	
}


bool Handler::sendMsg(const char* msg,int len)
{
	if(SOCKET_ERROR==send(clientSock,msg,len,0))
	{
		return false;
	}
	return true;
}

bool Handler::recvMsg()
{
	char recvBuf[MAX_RECV_BUF_SIZE];
	int bytesReceived = recv(clientSock,recvBuf,MAX_RECV_BUF_SIZE,0);
	
	printf("recv returned,received:[%d]\n",bytesReceived);
	if(bytesReceived == 0)
	{
		printf("connection has been closed!\n");
		onClose();
		return false;
	}
	else if(SOCKET_ERROR==bytesReceived)
	{
		DWORD dw = ::WSAGetLastError();
		assert(0);
		return false;
	}
	MemoryBlock block(recvBuf,bytesReceived);
	onReceiveMessage(block);
	return true;
}

